var express = require('express');
var router = express.Router();
var collectionDao = require('../dao/tao_acc_collection');
var collectionRouter = module.exports;

/**
 * 信息登记
 * @param  id
 * @param  account
 *         url//tao_acc_collection/register
 */
collectionRouter.register = function(req, res) {
    var par = req.body;
    if(!par.id) return res.json(new ERR('参数有误请重试', 400));
    if(!par.account) return res.json(new ERR('手机号不能为空！', 400));
    collectionDao.register(par, function(err, data) {
        if(err) {
            res.json(new ERR(err, 400));
        } else {
            res.json(new PKG(data));
        }
    });
}
router.post('/register', collectionRouter.register);

/**
 * 查看直接下级
 * @param  account
 * @param  size
 * @param  page
 *         url//tao_acc_collection/getSubList
 */
collectionRouter.getSubList = function(req, res) {
    var par = req.body;
    if(!par.account) return res.json(new ERR('账号不为空！', 400));
    if(!par.size) par.size = 10;
    par.size = parseInt(par.size);
    if(!par.page) {
        par.start = 0;
    } else {
        par.start = (parseInt(par.page) - 1) * parseInt(par.size);
    }
    collectionDao.getSubList(par, function(err, data) {
        if(err) {
            res.json(new ERR(err, 400));
        } else {
            res.json(new PKG(data));
        }
    });
}
router.post('/getSubList', collectionRouter.getSubList);

module.exports = router;